package com.cnn.test;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import java.util.HashMap;


public class MyUnityActivity extends UnityPlayerActivity implements View.OnClickListener {

    private Button topButton;
    private ImageButton quit;
    private Button video, video_lost;
    private String mEventId, mValue;
    private VideoView videoView;
    private Button big, small;
    private Uri uri;
    private RelativeLayout video_layout;
    public static int screenWidth, screenHeight;
    private LayoutInflater inflater;
    private View video_view;
    private PopupWindow pop;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_unity);

        inflater = LayoutInflater.from(this);
        video_view = inflater.inflate(R.layout.video_view, null);

        View playerView = mUnityPlayer.getView();
        video = (Button) findViewById(R.id.video);
        video.setOnClickListener(this);
        //video_lost=(Button)findViewById(R.id.video_lost);
        // video_lost.setOnClickListener(this);
        LinearLayout ll = (LinearLayout) findViewById(R.id.unityViewLyaout);
        ll.addView(playerView);

        //视频播放
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;


		/*quit = (ImageButton) findViewById(R.id.quit);
        quit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AlertDialog.Builder dialog=new AlertDialog.Builder(MyUnityActivity.this);
				dialog.setMessage("确认退出吗？");
				dialog.setPositiveButton("退出", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
						dialog.dismiss();
						AndroidTakeEvent();
					}
				});
				dialog.setNegativeButton("返回", null);
				dialog.show();
			}
		});*/
    }

    public void AndroidTakeEvent() {
        System.out.println("shao--------AndroidTakeEvent---------->>>>>> quit!!!!!");

        mHandler.post(mThread);
        //mThread.start();
    }

    public void AndroidVirUmengIIS(String eventId, String value) {
        mEventId = eventId;
        mValue = value;
        mHandler.postDelayed(thread, 150);
        //MobclickAgent.onEvent(this, eventId, label);

    }

    public void DataStatistics(String eventId, String value) {
        System.out.println("shao--------AndroidVirUmengIIS-------->>>>>eventId = " + eventId + "--->>>value = " + value);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(eventId, value);
        //MobclickAgent.onEvent(this, eventId, map);
        //MobclickAgent.onEvent(this, eventId, label);

    }

    Thread mThread = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            //mUnityPlayer.pause();
            mUnityPlayer.quit();
            thread.interrupt();
            finish();
        }
    });

    Thread thread = new Thread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            //mUnityPlayer.pause();
            DataStatistics(mEventId, mValue);
        }
    });
    Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
        }

    };

    private void displayBriefMemory() {
        final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(info);

    }

    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        //mUnityPlayer.quit();
        mUnityPlayer.pause();
        new AlertDialog.Builder(this)
                .setMessage("确定要退出3D演示吗？")
                .setNegativeButton("不退出", null)
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        mUnityPlayer.quit();
                    }
                }).show();
    }
	
	/*public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			//mUnityPlayer.quit();
			System.out.println("shao*******3333333333333333**************");
			sendBroadcast(new Intent("finish"));
		}
		System.out.println("shao*******444444444**************");
		return super.onKeyDown(keyCode, event); 
	}*/

	/* (non-Javadoc)
	 * @see com.unity3d.player.UnityPlayerNativeActivity#onPause()
	 */


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        //DialogActivity.this.finish();
       // MobclickAgent.onPageEnd("MyUnityActivityScreen");
       // MobclickAgent.onPause(this);
    }

    /* (non-Javadoc)
     * @see com.unity3d.player.UnityPlayerNativeActivity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
       // MobclickAgent.onPageStart("MyUnityActivityScreen");
       // MobclickAgent.onResume(this);
    }

    /* (non-Javadoc)
     * @see com.unity3d.player.UnityPlayerNativeActivity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int arg0, KeyEvent arg1) {
        // TODO Auto-generated method stub
        System.out.println("shao*******444444444**************");
        return super.onKeyDown(arg0, arg1);
    }

    public boolean onTouchEvent(MotionEvent event) {
        //System.out.println("shao-------------->>>>>>>> 有触摸事件发生 ！！！   event.getX() = "+event.getX()+"--->>> event.getY() = "+event.getY());

        return mUnityPlayer.injectEvent(event);
    }

    @Override
    public void onClick(View v) {
        // RelativeLayout.LayoutParams linearParams =  (RelativeLayout.LayoutParams)video_layout.getLayoutParams();

        /*if (v == big) {
            linearParams.height = screenWidth;
            linearParams.width=screenHeight;
            big.setVisibility(View.GONE);
            small.setVisibility(View.VISIBLE);
            video_layout.setLayoutParams(linearParams);
        }
        if (v == small) {
            linearParams.height = dp2px(this,200);
            linearParams.width=dp2px(this,200);
            big.setVisibility(View.VISIBLE);
            small.setVisibility(View.GONE);
            video_layout.setLayoutParams(linearParams);
        }*/
        if (v == video) {
            //  Dialog dialog=new Dialog(MyUnityActivity.this);
            //  dialog.setContentView(video_view);
            //   dialog.show();
            Intent intent = new Intent(this, VideoActivity.class);
            startActivity(intent);
        }


    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
